﻿Imports Nocelab.MSCRM
Imports System.Collections
Imports System.Drawing

Public Class CustomTaskPane
    Private _sheet As Excel.Worksheet
    Private _crmHelper As CRMHelper
    Private _testCRMConnection As Boolean

    Private Sub CreateCrmHelper()
        If _testCRMConnection = False Then
            If _crmHelper Is Nothing Then
                _crmHelper = New CRMHelper(txtCrmServer.Text, txtOrganization.Text, txtUsername.Text, txtPassword1.Text, txtDomain.Text)
            End If
        End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Globals.ThisAddIn.Application.StatusBar = "Loading Marketing List..."
        Dim mkList As ArrayList
        Dim x As Integer
        CreateCrmHelper()

        Try
            mkList = _crmHelper.GetMarketingList()

            For x = 0 To mkList.Count - 1
                cbMarketingList.DisplayMember = "value"
                cbMarketingList.ValueMember = "id"
                Dim ite As New MyItem(mkList.Item(x)(0), mkList.Item(x)(1))
                cbMarketingList.Items.Add(ite)
            Next

            cbMarketingList.SelectedItem = 1

            Globals.ThisAddIn.Application.StatusBar += "Done!"
        Catch ex As Exception
            MsgBox(ex.Message)
            Globals.ThisAddIn.Application.StatusBar += "Failed!"

        End Try
        

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        _sheet = Globals.ThisAddIn.Application.ActiveSheet()


        Dim accountColumn As Integer
        Dim contactColumn As Integer
        Dim leadColumn As Integer
        Dim currentImportColumn As Integer ' identify the column to import relate with the selected member type
        Dim memberType As MyItem
        Dim mklist As MyItem

        accountColumn = 1
        contactColumn = 3
        leadColumn = 5


        mklist = cbMarketingList.SelectedItem
        memberType = cbMemberType.SelectedItem

        Select Case memberType.value.ToString().ToLower()
            Case "account"
                currentImportColumn = accountColumn
            Case "contact"
                currentImportColumn = contactColumn
            Case "lead"
                currentImportColumn = leadColumn
        End Select

        If (mklist Is Nothing) Then
            MsgBox("Marketing List is mandatory")
            Return
        End If

        Globals.ThisAddIn.Application.StatusBar = "Importing " & memberType.value.ToString() & " members..."

        If mklist.id <> String.Empty And memberType.id <> String.Empty Then

            CreateCrmHelper()
            Dim marketingListId As New Guid(mklist.id)

            Dim a As String
            Dim retInsertRow As Boolean
            Dim message As String = ""
            Dim cRange As Excel.Range

            ' Start from row 2. The first row is for the header
            For row = 2 To _sheet.Rows.Count
                retInsertRow = False

                ' Get the cell value of the right column according with the selected member type
                a = _sheet.Cells(row, currentImportColumn).Value()

                If a = String.Empty Then
                    Exit For
                Else
                    ' Try to import the member
                    retInsertRow = _crmHelper.AddMemberToList(memberType.id, marketingListId, a, message)
                    _sheet.Cells(row, currentImportColumn + 1).Value = message

                    If (message.StartsWith("(Error)")) Then
                        ' In case of error set cell font as visible
                        cRange = _sheet.Cells(row, currentImportColumn)
                        cRange.Font.Bold = True
                        cRange.Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red)
                    End If

                End If
            Next
        End If
        'sheet.Cells(10, 1).Value = "fatto!"
     
        


        Globals.ThisAddIn.Application.StatusBar += "Done!"


    End Sub

    Private Sub CustomTaskPane_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        ' Populate Member Type ComboBox
        cbMemberType.DisplayMember = "value"
        cbMemberType.ValueMember = "id"
        cbMemberType.Items.Add(New MyItem("lead", "Lead"))
        cbMemberType.Items.Add(New MyItem("contact", "Contact"))
        cbMemberType.Items.Add(New MyItem("account", "Account"))
        cbMemberType.SelectedIndex = 0



    End Sub

    Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        _testCRMConnection = False
        _crmHelper = Nothing
        Globals.ThisAddIn.Application.StatusBar = "Testing CRM configuration..."
        CreateCrmHelper()
        Dim exMessage As String
        _testCRMConnection = _crmHelper.TestCRMConnection(exMessage)
        If _testCRMConnection = False Then
            MsgBox(exMessage + Chr(13) + Chr(10) + "There are erros in the CRM configuration. Please, check it")
        Else
            MsgBox("Connection successfull!")
        End If

        Globals.ThisAddIn.Application.StatusBar += "Done!"


    End Sub

    Private Sub txtCrmServer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCrmServer.TextChanged
        _crmHelper = Nothing
    End Sub

    Private Sub txtOrganization_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtOrganization.TextChanged
        _crmHelper = Nothing
    End Sub

    Private Sub txtUsername_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtUsername.TextChanged
        _crmHelper = Nothing
    End Sub

    Private Sub txtPassword1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassword1.TextChanged
        _crmHelper = Nothing
    End Sub

    Private Sub txtDomain_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDomain.TextChanged
        _crmHelper = Nothing
    End Sub
End Class
